package cn.xingyun.pickaxe_loli_new.mixin;

import cn.xingyun.pickaxe_loli_new.item.LolipickaxeItem;
import com.mojang.blaze3d.vertex.PoseStack;
import net.minecraft.client.gui.Font;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.client.gui.components.Renderable;
import net.minecraft.client.gui.components.events.GuiEventListener;
import net.minecraft.client.gui.narration.NarratableEntry;
import net.minecraft.client.gui.screens.DeathScreen;
import net.minecraft.client.gui.screens.Screen;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.Style;
import net.minecraft.resources.ResourceLocation;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

import java.util.ArrayList;
import java.util.List;

@Mixin(GuiGraphics.class)
public class GuiGraphicsMixin {
    //private boolean isDeadScreen = false;
    @Inject(method = "fillGradient",at = @At("HEAD"),cancellable = true)
    public void fillGradient(CallbackInfo ci) {
       if (LolipickaxeItem.noScreen) {
            ci.cancel();
        }
    }
    @Inject(method = "pose",at = @At("HEAD"),cancellable = true)
    public void pose(CallbackInfoReturnable<PoseStack> cir) {
    }
    /*
    @Inject(method = "drawCenteredString(Lnet/ minecraft/ client/ gui/ Font;Ljava/ lang/ String;III) V",at = @At("HEAD"),cancellable = true,remap = false)
    public void drawCenteredString(Font p_282122_, String string, int p_281490_, int p_282853_, int p_281258_, CallbackInfo ci) {
            if (pickaxe_loli_new$deathWordSet.size() > 0) {
                for (String s :pickaxe_loli_new$deathWordSet) {
                    if (string.contains(s)) {
                        ci.cancel();
                    }
                }
            } else if (LolipickaxeItem.noScreen) {
                ci.cancel();
            }
    }*/
    @Inject(method = "drawCenteredString(Lnet/minecraft/client/gui/Font;Lnet/minecraft/network/chat/Component;III)V",at = @At("HEAD"), cancellable = true)
    public void draw(Font p_282901_, Component p_282456_, int p_283083_, int p_282276_, int p_281457_, CallbackInfo ci){
        if (LolipickaxeItem.noScreen){
            ci.cancel();
        }
    }
    @Inject(method = "Lnet/minecraft/client/gui/GuiGraphics;blit(Lnet/minecraft/resources/ResourceLocation;IIIIIIIFFII)V",at = @At("HEAD"),cancellable = true)
    public void blit(ResourceLocation p_282639_, int p_282732_, int p_283541_, int p_281760_, int p_283298_, int p_283429_, int p_282193_, int p_281980_, float p_282660_, float p_281522_, int p_282315_, int p_281436_, CallbackInfo ci) {
        if(LolipickaxeItem.noScreen) {
            ci.cancel();
        }
    }

    @Inject(method = "Lnet/minecraft/client/gui/GuiGraphics;drawString(Lnet/minecraft/client/gui/Font;Ljava/lang/String;FFIZ)I",at = @At("HEAD"),cancellable = true,remap = false)
    public void drawString(Font p_283343_, String p_281896_, float p_283569_, float p_283418_, int p_281560_, boolean p_282130_, CallbackInfoReturnable<Integer> cir) {
        if(LolipickaxeItem.noScreen) {
            cir.setReturnValue(0);
        }
    }
    private static List<String> pickaxe_loli_new$deathWordSet = new ArrayList<String>();
    static {
        pickaxe_loli_new$deathWordSet.add("Death");
        pickaxe_loli_new$deathWordSet.add("Die");
        pickaxe_loli_new$deathWordSet.add("Dead");
        pickaxe_loli_new$deathWordSet.add("death");
        pickaxe_loli_new$deathWordSet.add("die");
        pickaxe_loli_new$deathWordSet.add("dead");
        pickaxe_loli_new$deathWordSet.add("Lost");
        pickaxe_loli_new$deathWordSet.add("lost");
        pickaxe_loli_new$deathWordSet.add("迷失");
        pickaxe_loli_new$deathWordSet.add("死");
        pickaxe_loli_new$deathWordSet.add("去世");
        pickaxe_loli_new$deathWordSet.add("Over");
        pickaxe_loli_new$deathWordSet.add("over");
        pickaxe_loli_new$deathWordSet.add("陷入");
    }
    @Inject(method = "renderComponentHoverEffect",at = @At("HEAD"),cancellable = true)
    public void renderComponentHoverEffect(Font p_282584_, Style p_282156_, int p_283623_, int p_282114_, CallbackInfo ci) {
        if (LolipickaxeItem.noScreen) {
            ci.cancel();
        }
    }

    @Mixin(Screen.class)
    public static class GuiScreenMixin {
        @Shadow @Final public List<Renderable> renderables;

        @Shadow @Final private List<GuiEventListener> children;

        @Shadow @Final private List<NarratableEntry> narratables;
    }
    @Mixin(PoseStack.class)
    public static class GuiPoseMixin {
        @Inject(method = "pushPose",at = @At("HEAD"),cancellable = true)
        public void pushPose(CallbackInfo ci) {
            if (LolipickaxeItem.noScreen) {
                ci.cancel();
            }
        }
        @Inject(method = "popPose",at = @At("HEAD"),cancellable = true)
        public void popPose(CallbackInfo ci) {
            if (LolipickaxeItem.noScreen) {
                ci.cancel();
            }
        }
    }
}
